package com.lu.core.auto;

import cn.hutool.core.util.StrUtil;
import com.lu.core.annotations.EnableLog;
import com.lu.core.aop.SystemLogAop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @program RCS
 * @description: 日志配置开启
 * @author: zhanglu
 * @create: 2021-03-09 15:59:00
 */
@Slf4j
@Configuration
public class LogProxyAutoConfiguration implements ImportAware {

    /**
     * 启动注解参数
     */
    public static AnnotationAttributes enableLog;

    @Override
    public void setImportMetadata(AnnotationMetadata importMetadata) {
        try {
            //获取注解参数
            this.enableLog = AnnotationAttributes.fromMap(
                    importMetadata.getAnnotationAttributes(EnableLog.class.getName(), false));
           if (this.enableLog == null) {
                log.info("@EnableLog is not present on importing class");
                return;
            }
           String platform = LogProxyAutoConfiguration.enableLog.getString("platform");
           log.info(StrUtil.format("{}日志开启", platform));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //请求响应日志切面
    @Bean
    public SystemLogAop logAop(){
        return enableLog == null ? null : new SystemLogAop();
    }

}
